package com.itdaan.portal.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * @author www.itdaan.com
 */
public class ApplicationProperty {

	private static Logger log = LoggerFactory.getLogger(ApplicationProperty.class);
	
	private static Properties properties = null;

	private static final String DEFAULT_FILE = "/application.properties";
	
	public static void main(String[] args) {
		init();
		System.out.println(ApplicationProperty.getProperty("solr.server.post"));
	}
	
	public static void setProperties(Properties properties) {
		ApplicationProperty.properties = properties;
	}

	// 静态读入属性文件到properties变量中
	public static void init() {
		if(properties!=null){
			return;
		}
		properties = new Properties();
		staticProper=new HashMap<String, Map<String,String>>();//清除键对值
		InputStream in = null;
		try {
			ApplicationProperty property = new ApplicationProperty();
			in = property.getClass().getResourceAsStream(DEFAULT_FILE);
			if (in != null) {
				properties.load(in);
			}
		} catch (IOException e) {
			log.error("load "+DEFAULT_FILE+" file error!");
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {}
			}
		}
	}

    /**
     * @description: 取config/property.properties文件对应的key值
     * @param:
     * @return:
     * @throws:
     */
	public static String getProperty(String key) {
		init();
		String value=properties.getProperty(key);
		/***if(value!=null){
			try {
				value= new String(value.getBytes(),ConfigConstants.SYSTEM_CODING);
			} catch (UnsupportedEncodingException e) {}
		}
		log.error("---value---:"+value);***/
		return value;
	}
	
	 /**
     * @description: 取config/property.properties文件对应的key值
     * @param:
     * @return:
     * @throws:
     */
	public static String getJSProperty(String key) {
		init();
		String value=properties.getProperty(key);
		if(value!=null){
			/***try {
				value = new String(value.getBytes(),ConfigConstants.SYSTEM_CODING);
				value = ("{'"+value+"'}").replace("|","','").replace(":","':'");
			} catch (UnsupportedEncodingException e) {}***/
			value = ("{'"+value+"'}").replace("|","','").replace(":","':'");
		}
		return value;
	}

	/**
     * @description: 取config/property.properties文件对应的key值
     * @param:defaultValue 如果key不存在返回的默认值
     * @return:
     * @throws:
     */
	public static String getProperty(String key, String defaultValue) {
		init();
		String value=properties.getProperty(key, defaultValue);
		/**if(value!=null){
			try {
				value= new String(value.getBytes(),ConfigConstants.SYSTEM_CODING);
			} catch (UnsupportedEncodingException e) {}
		}***/		
		return value;
	}
	
	/**
     * @description: 取config/property.properties文件对应的key值
     * @param:defaultValue 如果key不存在返回的默认值
     * @return:
     * @throws:
     */
	public static int getPropertyByInt(String key, int defaultValue) {
		init();
		String value=properties.getProperty(key, String.valueOf(defaultValue) );
		int rCode=defaultValue;
		try {
			rCode=Integer.parseInt(value)	;	
		} catch (Exception e) {
			log.error("value转INT失败", e);
			return rCode;
		}
		
		return rCode;
	}
	
	private static Map<String, Map<String,String>> staticProper=new HashMap<String, Map<String,String>>();
	
	/**
	 * 
	 * @description: 转成key对value的map
	 * 1:成功|2:失败
	 * @param:
	 * @return:
	 * @throws:
	 */
	public static Map<String,String> getPropertyMap(String key) {
		Map<String,String> map=staticProper.get(key);//从静态map里取值
		if(map==null){
			if(key.endsWith("int")){
				map = new TreeMap<String, String>(new Comparator<String>() {
					public int compare(String str1, String str2) {
						return Integer.valueOf(str1.trim()).compareTo(Integer.valueOf(str2.trim()));
					}
				});
			}else{
				map = new TreeMap<String, String>(new Comparator<String>() {
					public int compare(String str1, String str2) {
						return str1.compareTo(str2);
					}
				});
			}
			
			String values=getProperty(key);
			if(values!=null){
				String[] array=values.split("\\|");//以"|"分开
				String[] kvArr;
				for(String kv:array){
					kvArr=kv.split(":");////key:value
					if(kvArr.length==2){
						map.put(kvArr[0], kvArr[1]);//加到Map
					}
				}
			}
			if(map.size()>0){
				staticProper.put(key, map);//放到静态map里
			}
		}
		return map;
	}
	
}
